<?php

/*
 * 
 */
abstract class Message {
	/*
	 * 
	 */ 
	public static function getInstance($xml) {
	 	$postObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
	 	switch ($postObj->MsgType) {
	 		case message_type_text:
	 			$msg = new TextMessage($postObj);
	 			break;
	 		case message_type_image:
	 			$msg = new ImageMessage($postObj);
	 			break;
	 		case message_type_location:
	 			$msg = new LocationMessage($postObj);
	 			break;
	 		case message_type_link:
	 			$msg = new LinkMessage($postObj);
	 			break;
	 		case message_type_event:
	 			$msg = new EventMessage($postObj);
	 			break;
	 		case message_type_voice:
	 			$msg = new VoiceMessage($postObj);
	 			break;
	 		case message_type_video:
	 			$msg = new VideoMessage($postObj);
	 			break;
			case message_type_shortvideo:
				$msg = new ShortVideoMessage($postObj);
				break;
	 		default:
	 			throw_exception('不存在的MsgType');			
	 	}
	 	
	 	$msg->msgType = $postObj->MsgType;
	 	return $msg;
	 }
	 
	 /*
	  * 根据当前信息返回指定类型的ReplyMessage
	  */
	 public function reply($type) {
	 	$replyMsg = ReplyMessage::getInstance($type);
	 	$replyMsg->toUserName = $this->fromUserName;
	 	$replyMsg->fromUserName = $this->toUserName;
	 	$replyMsg->createTime = time();
	 	return $replyMsg;
	 }
	 
	 /*
	  * 
	  */
	 function __construct($postObj) {
	 	$this->fromUserName = (string)$postObj->FromUserName;
	 	$this->toUserName = (string)$postObj->ToUserName;
	 	$this->msgType = (string)$postObj->MsgType;
	 	$this->msgId = (double)$postObj->MsgId;
	 	$this->createTime = date('Y-m-d H:i:s', (string)$postObj->CreateTime);
	 }
	 
	 /*
	  * 
	  */
	 public $fromUserName = '';
	 
	 /*
	  *
	 */
	 public $toUserName = '';	

	 /*
	  *
	 */
	 public $createTime = NULL;

	 /*
	  *
	 */
	 public $msgType = '';
	 
	 /*
	  *
	 */
	 public $msgId = 0;	 
	 
}

/*
 * 
 */
class TextMessage extends Message {
	/*
	 *
	*/
	function __construct($postObj) {
		parent::__construct($postObj);
		$this->content = (string)$postObj->Content;
	}

	public $content = '';
}

/*
 * 
 */
class ImageMessage extends Message {
	/*
	 *
	*/
	function __construct($postObj) {
		parent::__construct($postObj);
		$this->picUrl = (string)$postObj->PicUrl;
		$this->mediaId = (string)$postObj->MediaId;
	}

	public $picUrl = '';
}

/*
 *
*/
class LocationMessage extends Message {
	/*
	 *
	*/
	function __construct($postObj) {
		parent::__construct($postObj);
		$this->locationX = (string)$postObj->Location_X;
		$this->locationY = (string)$postObj->Location_Y;
		$this->scale = (string)$postObj->Scale;
		$this->label = (string)$postObj->Label;
	}

	public $locationX = 0.0;
	public $locationY = 0.0;
	public $scale = 0.0;
	public $label = '';

}

/*
 * 
 */
class LinkMessage extends Message {
	/*
	 *
	*/
	function __construct($postObj) {
		parent::__construct($postObj);
		$this->title = (string)$postObj->Title;
		$this->description = (string)$postObj->Description;
		$this->url = (string)$postObj->Url;
	}

	public $title = '';
	public $description = '';
	public $url = '';
}

/*
 *
*/
class EventMessage extends Message {
	/*
	 *
	*/
	function __construct($postObj) {
		parent::__construct($postObj);
		$this->event = (string)$postObj->Event;
		$this->eventKey = (string)$postObj->EventKey;
		$this->ticket = (string)$postObj->Ticket;
		$this->msgid = (float)$postObj->MsgID;
		$this->status = (int)$postObj->Status;
		$this->totalcount = (int)$postObj->TotalCount;
		$this->filtercount = (int)$postObj->FilterCount;
		$this->sentcount = (int)$postObj->SentCount;
		$this->errorcount = (int)$postObj->ErrorCount;
		$this->createtime = (int)$postObj->CreateTime;
		$this->latitude = (int)$postObj->Latitude;
		$this->longitude = (int)$postObj->Longitude;
		$this->precision = (int)$postObj->Precision;
	}		
	public $event = '';
	public $eventKey = '';
	public $ticket = '';
	public $msgid = '';
	public $status = '';
	public $totalcount = '';
	public $filtercount = '';
	public $sentcount = '';
	public $errorcount = '';
	public $createtime = '';
	public $latitude = '';
	public $longitude = '';
	public $precision = '';
	
}


/*
 *
*/
class VoiceMessage extends Message {
	/*
	 *
	*/
	function __construct($postObj) {
		parent::__construct($postObj);
		$this->mediaId = (string)$postObj->MediaId;
		$this->format = (string)$postObj->Format;
	}

	public $mediaId = 0;
	public $format = '';
}



/*
 *
*/
class VideoMessage extends Message {
	/*
	 *
	*/
	function __construct($postObj) {
		parent::__construct($postObj);
		$this->mediaId = (string)$postObj->MediaId;
		$this->thumbMediaId = (string)$postObj->ThumbMediaId;
	}

	public $thumbMediaId = '';
}

class shortVideoMessage extends Message {
	/*
	 *
	*/
	function __construct($postObj) {
		parent::__construct($postObj);
		$this->mediaId = (string)$postObj->MediaId;
		$this->thumbMediaId = (string)$postObj->ThumbMediaId;
	}

	public $thumbMediaId = '';
}
?>